#ifndef __LOG_HPP__
#define __LOG_HPP__

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <ctime>

#define NORMAL  0
#define WARNING 1
#define ERROR   2
#define FATAL   3

const char* str[]={
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

void LogMessage(int level,const char* format,...)
{
    //标准日志
    char stdBuff[1024];
    bzero(stdBuff,sizeof(stdBuff));
    time_t tm=time(nullptr);
    struct tm* stm=localtime(&tm);
    char tms[128];
    bzero(tms,sizeof(tms));
    strftime(tms,sizeof(tms)-1,"%Y-%m-%d %H:%M:%S",stm);
    snprintf(stdBuff,sizeof(stdBuff),"[%s][%s]",str[level],tms);

    //自定义输出
    char logBuff[1024];
    bzero(logBuff,sizeof(logBuff));
    va_list ap;
    va_start(ap,format);
    vsnprintf(logBuff,sizeof (logBuff),format,ap);
    va_end(ap);

    //控制输出
#ifdef PRINT
    printf("%s%s",stdBuff,logBuff);
#else
    FILE* fp=fopen("./calcolator.log","a");
    fprintf(fp,"%s%s",stdBuff,logBuff);
    fclose(fp);
#endif
}

#endif